﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PriceLibrary.PriceService;

namespace VDataAdapter.PriceServerAdapter
{
    /// <summary>
    /// This class adapts callback from PriceServer to standard handler
    /// which contains correct type (so that no cast is required)
    /// </summary>
    class PriceServerEventAdapter
    {
        private IPriceServerEventHandler _handler;

        public PriceServerEventAdapter(IPriceServerEventHandler handler)
        {
            _handler = handler;
        }

        public void GetFirstTime(IDictionary<string, StockBase> basePrices, IDictionary<string, StockBase> top3Prices)
        {
            _handler.GetFirstTime(basePrices, top3Prices);
        }

        public void Transactions_Changed(IList<PriceLibrary.ChartService.Transaction> transactions, int countNumber, int totalCountNumber)
        {
            _handler.Transactions_Changed(transactions, countNumber, totalCountNumber);
        }

        /// <summary>
        /// Handle update top3Prices callback event from PriceServer
        /// This event will happen everytime there is a changed in StockBase: can be:
        /// - Change in Top3Price
        /// - Change in Stock Transaction (a.k.a 1 update o Khoi luong khop, gia khop)
        /// </summary>
        /// <param name="top3Prices"></param>
        public void Top3PricesStock_Changed(IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
        {
            _handler.Top3PricesStock_Changed(top3Prices);
        }

        public void MarketInfoChanged(IDictionary<int, StockBase> marketInfos)
        {
            var d = new Dictionary<int, MarketInfo>();
            foreach (KeyValuePair<int, StockBase> key in marketInfos)
            {
                d.Add(key.Key, (MarketInfo)key.Value);
            }
            _handler.MarketInfo_Changed(d);
        }

        public void PutThroughChanged(IList<StockBase> newstockbase)
        {
            var list = new List<StockInfoDyns>();
            foreach (StockBase s in newstockbase)
            {
                list.Add((StockInfoDyns)s);
            }
            _handler.PutThroughTopPrices_Changed(list);
        }

        public void PutThroughTransactionChanged(IList<StockBase> transactions)
        {
            var list = new List<Transaction>();
            foreach (StockBase s in transactions)
            {
                list.Add((Transaction)s);
            }
            _handler.PutThroughTransactions_Changed(list);
        }

    }
}
